package com.cos.shop.services.impl;

import com.cos.shop.common.ResponseResult;
import com.cos.shop.dao.CartDao;
import com.cos.shop.dao.ProductDao;
import com.cos.shop.dao.impl.CartDaoImpl;
import com.cos.shop.dao.impl.ProductDaoImpl;
import com.cos.shop.entity.Cart;
import com.cos.shop.entity.Product;
import com.cos.shop.services.CartServices;

import java.sql.SQLException;

/**
 * @author: COS
 * @time: 2023/5/11 11:32
 * @description:
 */
public class CartServicesImpl implements CartServices {
    private CartDao cd = new CartDaoImpl();
    private ProductDao pd = new ProductDaoImpl();

    @Override
    public ResponseResult changeCart(int uid, int pid, int num) {
        //1判断num 是否为0如果为0即为删除单条购物车数据
        try {
            if (num == 0) {
                cd.deleteCartByUidAnsPid(pid, uid);
            } else {
                //2 使用Pid 查询商品数据,重新计算小计
                Product pro = pd.getGoodsById(pid);
                //3 使用 uid +pid 查询购物车数据
                Cart cart = cd.getCartByUidAndPid(uid, pid);
                //4 进行修改  num=1  原有值+1, num=-1 原有值-1
                cart.setCnum(cart.getCnum()+num);
                // 重新计算小计价格
                cart.setCcount(cart.getCnum()*pro.getPprice());
                // 调用修改
                cd.updateCart(cart);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createSuccess();
    }

    @Override
    public ResponseResult clearCart(int pid, int uid) {
        String msg = "";
        int i = 0;
        try {
            if (pid == 0) {
                //1 判断pid 为0 调用情况用户的购物车方法
                i = cd.clearCart(uid);
                msg = "清空购物车成功";
            } else {
                //2不为0就删除单个用户单个购物车数据
                i = cd.deleteCartByUidAnsPid(pid, uid);
                msg = "移除购物车成功";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (i > 0) {
            return ResponseResult.createSuccess(msg);
        }
        return ResponseResult.createFail(msg);
    }

    @Override
    public ResponseResult getCartWithProByUid(int uid) {
        try {
            if (uid > 0) {
                return ResponseResult.createSuccess(cd.getCartWithProByUid(uid));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("没有查询到信息");
    }

    @Override
    public ResponseResult addCart(int uid, int goodsId) {
        try {
            //1 查询商品是否存在
            Product pro = pd.getGoodsById(goodsId);
            //判断是否存在
            if (pro == null) {
                return ResponseResult.createFail("没有改商品存在,请重新再试");
            }
            //2 使用uid + pid 查询购物车数据
            Cart c = cd.getCartByUidAndPid(uid, goodsId);
            //3 判断购物车是否存在
            int i = 0;
            if (c != null) {
                //4 购物车存在
                //5 修改数量和小计
                c.setCnum(c.getCnum() + 1);
                // 商品数量*商品单价
                c.setCcount(c.getCnum() * pro.getPprice());
                //6 修改
                i = cd.updateCart(c);
            } else {
                //7 购物车不存在
                //8 cart对象
                Cart cart = new Cart();
                //设置参数
                cart.setUid(uid);
                cart.setPid(pro.getPid());
                // 刚创建可的购物车数据 默认只有1个商品
                cart.setCnum(1);
                // 单个商品的单价就是小计
                cart.setCcount(pro.getPprice());
                //9 提交新增
                i = cd.addCart(cart);
            }
            if (i > 0) {
                return ResponseResult.createSuccess("购物车操作成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createSuccess("购物车操作失败,请重新再试");
    }
}

